package com.book.lizhi.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.database.DataBaseMannager;
import com.book.lizhi.model.ArticleModel;

public class ArticleAdapter extends AppAdapter {

	public ArticleAdapter(Context context, List<?> list) {
		super(context, list);
	}

	@Override
	public View getView(int position, View convertView, final Context context) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.story_item_layout, null);
			holder.storyNameView = (TextView) convertView.findViewById(R.id.story_name);
			holder.storyBriefView = (TextView) convertView.findViewById(R.id.story_brief);
			holder.favView = (TextView) convertView.findViewById(R.id.article_fav_view);
			holder.favContainer = (LinearLayout) convertView.findViewById(R.id.favorite_container);
			holder.shareView = (LinearLayout) convertView.findViewById(R.id.share_container);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final ArticleModel model = (ArticleModel) getItem(position);

		holder.storyNameView.setText(model.name);
		holder.storyBriefView.setText(model.brief);
		if (model.isFav) {
			holder.favView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_press_favorite, 0, 0, 0);
		} else {
			holder.favView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_favorite, 0, 0, 0);
		}
		holder.favContainer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (model.isFav) {
					new AlertDialog.Builder(context).setTitle("取消收藏").setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							holder.favView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_favorite, 0, 0, 0);
							model.isFav = false;
							DataBaseMannager.getInstance().deleteFavById(context, model.id);
							Intent intent = new Intent(Constants.DELETE_FAV);
							intent.putExtra("id", model.id);
							LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
						}
					}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}).show();
				} else {
					holder.favView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_press_favorite, 0, 0, 0);
					model.isFav = true;
					DataBaseMannager.getInstance().insertFave(context, model);
					Toast.makeText(context, "收藏成功!", Toast.LENGTH_SHORT).show();
				}
			}
		});

		holder.shareView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent sendIntent = new Intent();
				sendIntent.setAction(Intent.ACTION_SEND);
				sendIntent.putExtra(Intent.EXTRA_TEXT, "我在励志应用中看到一篇很有用的文章《" + model.name + "》下载励志应用看完整文章。http://www.wandoujia.com/apps/com.book.lizhi");
				sendIntent.setType("text/plain");
				context.startActivity(Intent.createChooser(sendIntent, "励志文章分享"));

			}
		});

		return convertView;
	}

	static class ViewHolder {
		TextView storyNameView;
		TextView storyBriefView;
		TextView favView;
		LinearLayout favContainer;
		LinearLayout shareView;

	}

}
